﻿using Lg.Core.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Lg.Core.Validate
{
    public static class Extensions
    {
        #region [requird]
        /// <summary>
        /// 非空验证，错误直接抛LgException异常，可自定义异常内容，较验通过则返回原对像
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="throwErrorMsg"></param>
        /// <returns></returns>
        /// <exception cref="LgException"></exception>
        public static string Requird<T>(this T t, string throwErrorMsg = "必填项不可为空!")
        {
            var str = t==null?null: Convert.ToString(t);
            bool b = Verifier.Required(str);
            if (!b)
            {
                throw new LgException(throwErrorMsg);
            }
            return str!;
        }
        /// <summary>
        /// 非空验证，当验证未通过时执行自定义函数，函数接收一个验证结果的布尔值，较验通过则返回原对像
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public static T Requird<T>(this T t, Action<bool> action)
        {
            bool b = Verifier.Required(Convert.ToString(t));
            if (!b)
            {
                action(b);
            }
            return t;
        }
        #endregion

        #region [IsPhone]
        public static string IsPhone(this string str, string throwErrorMsg = "不是有效的手机号码！")
        {
            bool res = Regex.IsMatch(str, "^1[3-9][0-9]{9}$");
            if (!res)
            {
                throw new LgException(throwErrorMsg);
            }
            return str;
        }
        public static string IsPhone(this string str, Action<bool> action)
        {
            bool b = Regex.IsMatch(str, "^1[3-9][0-9]{9}$");
            if (!b)
            {
                action(b);
            }
            return str;
        }
        #endregion

        #region [IsNumber]
        public static double IsNumber(this string str, string throwErrorMsg = "不是有效的数字类型！")
        {
            bool res = Verifier.IsNumber(str);
            if (!res)
            {
                throw new LgException(throwErrorMsg);
            }
            return Convert.ToDouble(str);
        }
        public static double IsNumber(this string str, Action<bool> action)
        {
            bool b = Verifier.IsNumber(str);
            if (!b)
            {
                action(b);
            }
            return double.Parse(str);
        }
        #endregion

        #region [InRange]
        public static double InRange(this double d, double[] range, string msg = "取值不在指定范围内！")
        {
            bool b= Verifier.InRange(d, range);
            if (!b)
            {
                throw new LgException(msg);
            }
            return d;
        }
        public static double InRange(this double d, double[] range, Action<bool> action)
        {
            bool b = Verifier.InRange(d, range);
            if (!b)
            {
                action(b);
            }
            return d;
        }
        public static double InRange(this double d, RangeOptions option, double[] range, string msg = "取值不在指定范围内！")
        {
            bool b = Verifier.InRange(d,option,range);
            if (!b)
            {
                throw new LgException(msg);
            }
            return d;
        }
        public static double InRange(this double d, RangeOptions option, double[] range, Action<bool> action)
        {
            bool b = Verifier.InRange(d, option, range);
            if (!b)
            {
                action(b);
            }
            return d;
        }
        #endregion

        #region [IsStronger]
        public static string IsStrong(this string str, string msg = "密码强度较验失败！")
        {
            bool b = Verifier.IsStrong(str);
            if (!b)
            {
                throw new LgException(msg);
            }
            return str;
        }
        #endregion

    }
}
